home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / NetHack 3.1.3 / source / src / artifact.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  29.6 KB  |  1,130 lines  |  [TEXT/R*ch]

  1. /*    SCCS Id: @(#)artifact.c    3.1    93/05/25    */
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #include "hack.h"
  6. #include "artifact.h"
  7. #ifdef OVLB
  8. #include "artilist.h"
  9. #else
  10. STATIC_DCL struct artifact artilist[];
  11. #endif
  12. /*
  13.  * Note:  both artilist[] and artiexist[] have a dummy element #0,
  14.  *      so loops over them should normally start at #1.  The primary
  15.  *      exception is the save & restore code, which doesn't care about
  16.  *      the contents, just the total size.
  17.  */
  18.  
  19. extern boolean notonhead;    /* for long worms */
  20.  
  21. #define get_artifact(o) \
  22.         (((o)&&(o)->oartifact) ? &artilist[(int) (o)->oartifact] : 0)
  23. STATIC_DCL int FDECL(spec_applies, (const struct artifact*,struct permonst*));
  24. STATIC_DCL int FDECL(arti_invoke, (struct obj*));
  25.  
  26. #ifndef OVLB
  27. STATIC_DCL int spec_dbon_applies;
  28.  
  29. #else    /* OVLB */
  30. /* coordinate effects from spec_dbon() with messages in artifact_hit() */
  31. STATIC_OVL int spec_dbon_applies = 0;
  32.  
  33. /* flags including which artifacts have already been created */
  34. static boolean artiexist[1+NROFARTIFACTS+1];
  35.  
  36. static void NDECL(hack_artifacts);
  37. static boolean FDECL(attacks, (int,struct obj *));
  38.  
  39. /* handle some special cases; must be called after u_init() */
  40. static void
  41. hack_artifacts()
  42. {
  43.     /* Excalibur can be used by any lawful character, not just knights */
  44.     if (pl_character[0] != 'K')
  45.         artilist[ART_EXCALIBUR].class = '\0';
  46. #ifdef MULDGN
  47.     /* Mitre of Holiness has same alignment as priest starts out with */
  48.     if (pl_character[0] == 'P')
  49.         artilist[ART_MITRE_OF_HOLINESS].alignment = u.ualignbase[1];
  50. #endif
  51. }
  52.  
  53. /* zero out the artifact existence list */
  54. void
  55. init_artifacts()
  56. {
  57.     (void) memset((genericptr_t) artiexist, 0, sizeof artiexist);
  58.     hack_artifacts();
  59. }
  60.  
  61. void
  62. save_artifacts(fd)
  63. int fd;
  64. {
  65.     bwrite(fd, (genericptr_t) artiexist, sizeof artiexist);
  66. }
  67.  
  68. void
  69. restore_artifacts(fd)
  70. int fd;
  71. {
  72.     mread(fd, (genericptr_t) artiexist, sizeof artiexist);
  73.     hack_artifacts();    /* redo non-saved special cases */
  74. }
  75.  
  76. const char *
  77. artiname(artinum)
  78. int artinum;
  79. {
  80.     if (artinum <= 0 || artinum > NROFARTIFACTS) return("");
  81.     return(artilist[artinum].name);
  82. }
  83.  
  84. /*
  85.    Make an artifact.  If a specific alignment is specified, then an object of
  86.    the appropriate alignment is created from scratch, or NULL is returned if
  87.    none is available.  If no alignment is given, then 'otmp' is converted
  88.    into an artifact of matching type, or returned as-is if that's not possible.
  89.    For the 2nd case, caller should use ``obj = mk_artifact(obj, A_NONE);''
  90.    for the 1st, ``obj = mk_artifact(NULL, some_alignment);''.
  91.  */
  92. struct obj *
  93. mk_artifact(otmp, alignment)
  94. struct obj *otmp;    /* existing object; ignored if alignment specified */
  95. aligntyp alignment;    /* target alignment, or A_NONE */
  96. {
  97.     register const struct artifact *a;
  98.     register int n = 0, m;
  99.     register boolean by_align = (alignment != A_NONE);
  100.     register short o_typ = (by_align || !otmp) ? 0 : otmp->otyp;
  101.     boolean unique = !by_align && otmp && objects[o_typ].oc_unique;
  102.  
  103.     /* count eligible artifacts */
  104.     for (a = artilist+1,m = 1; a->otyp; a++,m++)
  105.         if ((by_align ? a->alignment == alignment : a->otyp == o_typ) &&
  106.         (!(a->spfx & SPFX_NOGEN) || unique) && !artiexist[m]) {
  107.         if (by_align && a->class == pl_character[0])
  108.             goto make_artif;    /* 'a' points to the desired one */
  109.         else
  110.             n++;
  111.         }
  112.  
  113.     if (n) {        /* found at least one candidate */
  114.         /* select one, then find it again */
  115.         if (n > 1) n = rnd(n);    /* [1..n] */
  116.         for (a = artilist+1,m = 1; a->otyp; a++,m++)
  117.         if ((by_align ? a->alignment == alignment : a->otyp == o_typ)&&
  118.             (!(a->spfx & SPFX_NOGEN) || unique) && !artiexist[m]) {
  119.             if (!--n) break;    /* stop when chosen one reached */
  120.         }
  121.  
  122.         /* make an appropriate object if necessary, then christen it */
  123. make_artif: if (by_align) otmp = mksobj((int)a->otyp, TRUE, FALSE);
  124.         otmp = oname(otmp, a->name, 0);
  125.         otmp->oartifact = m;
  126.         artiexist[m] = TRUE;
  127.     } else {
  128.         /* nothing appropriate could be found; return the original object */
  129.         if (by_align) otmp = 0;    /* (there was no original object) */
  130.     }
  131.     return otmp;
  132. }
  133.  
  134. /*
  135.  * Returns the full name (with articles and correct capitalization) of an
  136.  * artifact named "name" if one exists, or NULL, it not.
  137.  * The given name must be rather close to the real name for it to match.
  138.  * The object type of the artifact is returned in otyp if the return value
  139.  * is non-NULL.
  140.  */
  141. const char*
  142. artifact_name(name, otyp)
  143. const char *name;
  144. short *otyp;
  145. {
  146.     register const struct artifact *a;
  147.     register const char *aname;
  148.  
  149.     if(!strncmpi(name, "the ", 4)) name += 4;
  150.  
  151.     for (a = artilist+1; a->otyp; a++) {
  152.     aname = a->name;
  153.     if(!strncmpi(aname, "the ", 4)) aname += 4;
  154.     if(!strcmpi(name, aname)) {
  155.         *otyp = a->otyp;
  156.         return a->name;
  157.     }
  158.     }
  159.  
  160.     return NULL;
  161. }
  162.  
  163. boolean
  164. exist_artifact(otyp, name)
  165. register int otyp;
  166. register const char *name;
  167. {
  168.     register const struct artifact *a;
  169.     register boolean *arex;
  170.  
  171.     if (otyp && *name)
  172.         for (a = artilist+1,arex = artiexist+1; a->otyp; a++,arex++)
  173.         if ((int) a->otyp == otyp && !strcmp(a->name, name))
  174.             return *arex;
  175.     return FALSE;
  176. }
  177.  
  178. void
  179. artifact_exists(otmp, name, mod)
  180. register struct obj *otmp;
  181. register const char *name;
  182. register boolean mod;
  183. {
  184.     register const struct artifact *a;
  185.  
  186.     if (otmp && *name)
  187.         for (a = artilist+1; a->otyp; a++)
  188.         if (a->otyp == otmp->otyp && !strcmp(a->name, name)) {
  189.             register int m = a - artilist;
  190.             otmp->oartifact = (char)(mod ? m : 0);
  191.             otmp->age = 0;
  192.             if(otmp->otyp == RIN_INCREASE_DAMAGE)
  193.             otmp->spe = 0;
  194.             artiexist[m] = mod;
  195.             break;
  196.         }
  197.     return;
  198. }
  199.  
  200. int
  201. nartifact_exist()
  202. {
  203.     int a = 0;
  204.     int n = SIZE(artiexist);
  205.  
  206.     while(n > 1)
  207.     if(artiexist[--n]) a++;
  208.  
  209.     return a;
  210. }
  211.  
  212. /*
  213.  * This function is used to un-create an artifact.  Normally, when an artifact
  214.  * is destroyed, it cannot be re-created by any means.  However, if you call
  215.  * this function _before_ destroying the object, then the artifact can be
  216.  * re-created later on.  Currently used only by the wish code.
  217.  */
  218. void
  219. artifact_unexist(otmp)
  220.     register struct obj *otmp;
  221. {
  222.     if (otmp->oartifact && artiexist[(int)otmp->oartifact])
  223.     artiexist[(int)otmp->oartifact] = 0;
  224.     else
  225.     impossible("Destroying non-existing artifact?!");
  226. }
  227.  
  228. #endif /* OVLB */
  229. #ifdef OVL0
  230.  
  231. boolean
  232. spec_ability(otmp, abil)
  233. struct obj *otmp;
  234. unsigned abil;
  235. {
  236.     const struct artifact *arti = get_artifact(otmp);
  237.  
  238.     return((boolean)(arti && (arti->spfx & abil)));
  239. }
  240.  
  241. #endif /* OVL0 */
  242. #ifdef OVLB
  243.  
  244. boolean
  245. restrict_name(otmp, name)  /* returns 1 if name is restricted for otmp->otyp */
  246. register struct obj *otmp;
  247. register const char *name;
  248. {
  249.     register const struct artifact *a;
  250.  
  251.     if (!*name) return FALSE;
  252.  
  253.         /* Since almost every artifact is SPFX_RESTR, it doesn't cost
  254.            us much to do the string comparison before the spfx check.
  255.            Bug fix:  don't name multiple elven daggers "Sting".
  256.          */
  257.     for (a = artilist+1; a->otyp; a++)
  258.         if (a->otyp == otmp->otyp && !strcmp(a->name, name))
  259.         return ((boolean)((a->spfx & (SPFX_NOGEN|SPFX_RESTR)) != 0 ||
  260.             otmp->quan > 1L));
  261.  
  262.     return FALSE;
  263. }
  264.  
  265. static boolean
  266. attacks(adtyp, otmp)
  267. register int adtyp;
  268. register struct obj *otmp;
  269. {
  270.     register const struct artifact *weap;
  271.  
  272.     if ((weap = get_artifact(otmp)) != 0)
  273.         return((boolean)(weap->attk.adtyp == adtyp));
  274.     return(0);
  275. }
  276.  
  277. boolean
  278. defends(adtyp, otmp)
  279. register int adtyp;
  280. register struct obj *otmp;
  281. {
  282.     register const struct artifact *weap;
  283.  
  284.     if ((weap = get_artifact(otmp)) != 0)
  285.         return((boolean)(weap->defn.adtyp == adtyp));
  286.     return(0);
  287. }
  288.  
  289. /*
  290.  * a potential artifact has just been worn/wielded/picked-up or
  291.  * unworn/unwielded/dropped.  Pickup/drop only set/reset the W_ART mask.
  292.  */
  293. void
  294. set_artifact_intrinsic(otmp,on,wp_mask)
  295. register struct obj *otmp;
  296. boolean on;
  297. long wp_mask;
  298. {
  299.     long *mask = 0;
  300.     register const struct artifact *oart = get_artifact(otmp);
  301.     uchar dtyp;
  302.     long spfx;
  303.     
  304.     if (!oart) return;
  305.  
  306.     /* effects from the defn field */
  307.     dtyp = (wp_mask != W_ART) ? oart->defn.adtyp : oart->cary.adtyp;
  308.  
  309.     if (dtyp == AD_FIRE)
  310.         mask = &HFire_resistance;
  311.     else if (dtyp == AD_COLD)
  312.         mask = &HCold_resistance;
  313.     else if (dtyp == AD_ELEC)
  314.         mask = &HShock_resistance;
  315.     else if (dtyp == AD_MAGM)
  316.         mask = &Antimagic;
  317.     else if (dtyp == AD_DISN)
  318.         mask = &HDisint_resistance;
  319.  
  320.     if(mask && wp_mask == W_ART && !on) {
  321.         /* find out if some other artifact also confers this intrinsic */
  322.         /* if so, leave the mask alone */
  323.         register struct obj* obj;
  324.         for(obj = invent; obj; obj = obj->nobj)
  325.         if(obj != otmp && obj->oartifact) {
  326.             register const struct artifact *art = get_artifact(obj);
  327.             if(art->cary.adtyp == dtyp) {
  328.             mask = (long *) 0;
  329.             break;
  330.             }
  331.         }
  332.     }
  333.     if(mask) {
  334.         if (on) *mask |= wp_mask;
  335.         else *mask &= ~wp_mask;
  336.     }
  337.  
  338.     /* intrinsics from the spfx field; there could be more than one */
  339.     spfx = (wp_mask != W_ART) ? oart->spfx : oart->cspfx;
  340.     if(spfx && wp_mask == W_ART && !on) {
  341.         /* don't change any spfx also conferred by other artifacts */
  342.         register struct obj* obj;
  343.         for(obj = invent; obj; obj = obj->nobj)
  344.         if(obj != otmp && obj->oartifact) {
  345.             register const struct artifact *art = get_artifact(obj);
  346.             spfx &= ~art->cspfx;
  347.         }
  348.     }
  349.  
  350.     if (spfx & SPFX_SEARCH) {
  351.         if(on) Searching |= wp_mask;
  352.         else Searching &= ~wp_mask;
  353.     }
  354.     if (spfx & SPFX_HALRES) {
  355.         /* make_hallucinated must (re)set the mask itself to get
  356.          * the display right */
  357.         make_hallucinated((long)!on, TRUE, wp_mask);
  358.     }
  359.     if (spfx & SPFX_ESP) {
  360.         if(on) HTelepat |= wp_mask;
  361.         else HTelepat &= ~wp_mask;
  362.         see_monsters();
  363.     }
  364.     if (spfx & SPFX_STLTH) {
  365.         if (on) Stealth |= wp_mask;
  366.         else Stealth &= ~wp_mask;
  367.     }
  368.     if (spfx & SPFX_REGEN) {
  369.         if (on) HRegeneration |= wp_mask;
  370.         else HRegeneration &= ~wp_mask;
  371.     }
  372.     if (spfx & SPFX_TCTRL) {
  373.         if (on) HTeleport_control |= wp_mask;
  374.         else HTeleport_control &= ~wp_mask;
  375.     }
  376.     if (spfx & SPFX_WARN) {
  377.         if (on) Warning |= wp_mask;
  378.         else Warning &= ~wp_mask;
  379.     }
  380.     if (spfx & SPFX_EREGEN) {
  381.         if (on) Energy_regeneration |= wp_mask;
  382.         else Energy_regeneration &= ~wp_mask;
  383.     }
  384.     if (spfx & SPFX_HSPDAM) {
  385.         if (on) Half_spell_damage |= wp_mask;
  386.         else Half_spell_damage &= ~wp_mask;
  387.     }
  388.     if (spfx & SPFX_HPHDAM) {
  389.         if (on) Half_physical_damage |= wp_mask;
  390.         else Half_physical_damage &= ~wp_mask;
  391.     }
  392.  
  393.     if(wp_mask == W_ART && !on && oart->inv_prop) {
  394.         /* might have to turn off invoked power too */
  395.         if (oart->inv_prop <= LAST_PROP &&
  396.         (u.uprops[oart->inv_prop].p_flgs & W_ARTI))
  397.         (void) arti_invoke(otmp);
  398.     }
  399. }
  400.  
  401. /*
  402.  * creature (usually player) tries to touch (pick up or wield) an artifact obj.
  403.  * Returns 0 if the object refuses to be touched.
  404.  * This routine does not change any object chains.
  405.  * Ignores such things as gauntlets, assuming the artifact is not
  406.  * fooled by such trappings.
  407.  */
  408. int
  409. touch_artifact(obj,mon)
  410.     struct obj *obj;
  411.     struct monst *mon;
  412. {
  413.     register const struct artifact *oart = get_artifact(obj);
  414.     boolean badclass, badalign;
  415.     boolean yours = (mon == &youmonst);
  416.  
  417.     if(!oart) return 1;
  418.  
  419.     badclass = (oart->class && (!yours || oart->class != pl_character[0]));
  420.     badalign = (oart->spfx & SPFX_RESTR) &&
  421.     ((oart->alignment !=
  422.       (yours ? u.ualign.type : sgn(mon->data->maligntyp))) ||
  423.      (yours && u.ualign.record < 0));
  424.  
  425.     if(((badclass || badalign) && (oart->spfx & SPFX_INTEL)) ||
  426.        (badalign && (!yours || !rn2(4))))  {
  427.     int dmg;
  428.     char buf[BUFSZ];
  429.  
  430.     if (!yours) return 0;
  431.     pline("You are blasted by %s power!", s_suffix(the(xname(obj))));
  432.     dmg = d((Antimagic ? 2 : 4) , ((oart->spfx & SPFX_INTEL) ? 10 : 4));
  433.     Sprintf(buf, "touching %s", oart->name);
  434.     losehp(dmg, buf, KILLED_BY);
  435.     exercise(A_WIS, FALSE);
  436.     }
  437.  
  438.     /* can pick it up unless you're totally non-synch'd with the artifact */
  439.     if(badclass && badalign && (oart->spfx & SPFX_INTEL)) {
  440.     if (yours) pline("%s evades your grasp!", The(xname(obj)));
  441.     return 0;
  442.     }
  443.  
  444.     return 1;
  445. }
  446.  
  447. #endif /* OVLB */
  448. #ifdef OVL1
  449.  
  450. STATIC_OVL int
  451. spec_applies(weap, ptr)
  452. register const struct artifact *weap;
  453. struct permonst *ptr;
  454. {
  455.     boolean yours = (ptr == &playermon);
  456.  
  457.     if(!(weap->spfx & (SPFX_DBONUS | SPFX_ATTK)))
  458.         return(weap->attk.adtyp == AD_PHYS);
  459.  
  460.     if(weap->spfx & SPFX_DMONS)
  461.         return((ptr == &mons[(int)weap->mtype]));
  462.     else if(weap->spfx & SPFX_DCLAS)
  463.         return((weap->mtype == ptr->mlet));
  464.     else if(weap->spfx & SPFX_DFLAG1)
  465.         return((ptr->mflags1 & weap->mtype) != 0L);
  466.     else if(weap->spfx & SPFX_DFLAG2)
  467.         return((ptr->mflags2 & weap->mtype) != 0L);
  468.     else if(weap->spfx & SPFX_DALIGN)
  469.         return(ptr->maligntyp == A_NONE ||
  470.            sgn(ptr->maligntyp) != sgn(weap->alignment));
  471.     else if(weap->spfx & SPFX_ATTK) {
  472.         switch(weap->attk.adtyp) {
  473.         case AD_FIRE:
  474.             return(!(yours ? Fire_resistance : resists_fire(ptr)));
  475.         case AD_COLD:
  476.             return(!(yours ? Cold_resistance : resists_cold(ptr)));
  477.         case AD_ELEC:
  478.             return(!(yours ? Shock_resistance : resists_elec(ptr)));
  479.         case AD_MAGM:
  480.         case AD_STUN:
  481.             return(!(yours ? Antimagic : (rn2(101) < ptr->mr)));
  482.         case AD_DRLI:
  483.             if (!yours) return(!resists_drli(ptr));
  484.             else return(
  485. #ifdef POLYSELF
  486.                 resists_drli(uasmon) ||
  487. #endif
  488.                 defends(AD_DRLI, uwep));
  489.         case AD_STON:
  490. #ifdef POLYSELF
  491.             if (yours) return(!resists_ston(uasmon));
  492.             else
  493. #endif
  494.                 return(!resists_ston(ptr));
  495.         default:    impossible("Weird weapon special attack.");
  496.         }
  497.     }
  498.     return(0);
  499. }
  500.  
  501. int
  502. spec_abon(otmp, ptr)
  503. struct obj *otmp;
  504. struct permonst *ptr;
  505. {
  506.     register const struct artifact *weap;
  507.  
  508.     if ((weap = get_artifact(otmp)) != 0)
  509.         if(spec_applies(weap, ptr))
  510.             return((weap->attk.damn) ? rnd((int)weap->attk.damn) : 0);
  511.     return(0);
  512. }
  513.  
  514. int
  515. spec_dbon(otmp, ptr, tmp)
  516. register struct obj *otmp;
  517. register struct permonst *ptr;
  518. register int    tmp;
  519. {
  520.     register const struct artifact *weap;
  521.  
  522.     if ((weap = get_artifact(otmp)) != 0)
  523.         if ((spec_dbon_applies = spec_applies(weap, ptr)) != 0)
  524.         return((weap->attk.damd) ? rnd((int)weap->attk.damd) : tmp);
  525.     else spec_dbon_applies = 0;
  526.     return(0);
  527. }
  528.  
  529. #endif /* OVL1 */
  530.  
  531. #ifdef OVLB
  532.  
  533. /* Function used when someone attacks someone else with an artifact
  534.  * weapon.  Only adds the special (artifact) damage, and returns a 1 if it
  535.  * did something special (in which case the caller won't print the normal
  536.  * hit message).  This should be called once upon every artifact attack;
  537.  * dmgval() no longer takes artifact bonuses into account.  Possible
  538.  * extension: change the killer so that when an orc kills you with
  539.  * Stormbringer it's "killed by Stormbringer" instead of "killed by an orc".
  540.  */
  541. boolean
  542. artifact_hit(magr, mdef, otmp, dmgptr, dieroll)
  543. struct monst *magr, *mdef;
  544. struct obj *otmp;
  545. int *dmgptr;
  546. int dieroll; /* needed for Magicbane and vorpal blades */
  547. {
  548.     boolean youattack = (magr == &youmonst);
  549.     boolean youdefend = (mdef == &youmonst);
  550.     boolean vis = (!youattack && magr && cansee(magr->mx, magr->my))
  551.         || (!youdefend && cansee(mdef->mx, mdef->my));
  552.     boolean realizes_damage;
  553.  
  554.     static const char you[] = "you";
  555.     const char *hittee = youdefend ? you : mon_nam(mdef);
  556.  
  557.     /* The following takes care of most of the damage, but not all--
  558.      * the exception being for level draining, which is specially
  559.      * handled.  Messages are done in this function, however.
  560.      */
  561.     *dmgptr += spec_dbon(otmp, youdefend ? &playermon
  562.         : mdef->data, *dmgptr);
  563.  
  564.     if (youattack && youdefend) {
  565.         impossible("attacking yourself with weapon?");
  566.         return FALSE;
  567.     }
  568.  
  569.     realizes_damage = (youdefend || vis) && spec_dbon_applies;
  570.  
  571.     /* the four basic attacks: fire, cold, shock and missiles */
  572.     if (attacks(AD_FIRE, otmp)) {
  573.         if (realizes_damage) {
  574.             pline("The fiery blade burns %s!", hittee);
  575.             return TRUE;
  576.         }
  577.     }
  578.     if (attacks(AD_COLD, otmp)) {
  579.         if (realizes_damage) {
  580.             pline("The ice-cold blade freezes %s!", hittee);
  581.             return TRUE;
  582.         }
  583.     }
  584.     if (attacks(AD_ELEC, otmp)) {
  585.         if (realizes_damage) {
  586.             if(youattack && otmp != uwep)
  587.                 pline("%s hits %s!", The(xname(otmp)), hittee);
  588.             pline("Lightning strikes %s!", hittee);
  589.             return TRUE;
  590.         }
  591.     }
  592.     if (attacks(AD_MAGM, otmp)) {
  593.         if (realizes_damage) {
  594.             if(youattack && otmp != uwep)
  595.                 pline("%s hits %s!", The(xname(otmp)), hittee);
  596.             pline("A hail of magic missiles strikes %s!", hittee);
  597.             return TRUE;
  598.         }
  599.     }
  600.  
  601.     /*
  602.      * Magicbane's intrinsic magic is incompatible with normal
  603.      * enchantment magic.  Thus, its effects have a negative
  604.      * dependence on spe.  Against low mr victims, it typically
  605.      * does "double athame" damage, 2d4.  Occasionally, it will
  606.      * cast unbalancing magic which effectively averages out to
  607.      * 4d4 damage (2.5d4 against high mr victims), for spe = 0.
  608.      */
  609.  
  610. #define MB_MAX_DIEROLL        8    /* rolls above this aren't magical */
  611. #define MB_INDEX_INIT        (-1)
  612. #define MB_INDEX_PROBE        0
  613. #define MB_INDEX_STUN        1
  614. #define MB_INDEX_SCARE        2
  615. #define MB_INDEX_PURGE        3
  616. #define MB_RESIST_ATTACK    (resist_index = attack_index)
  617. #define MB_RESISTED_ATTACK    (resist_index == attack_index)
  618. #define MB_UWEP_ATTACK        (youattack && (otmp == uwep))
  619.  
  620.     if (attacks(AD_STUN, otmp) && (dieroll <= MB_MAX_DIEROLL)) {
  621.         int attack_index = MB_INDEX_INIT;
  622.         int resist_index = MB_INDEX_INIT;
  623.         int scare_dieroll = MB_MAX_DIEROLL / 2;
  624.  
  625.         if (otmp->spe >= 3)
  626.             scare_dieroll /= (1 << (otmp->spe / 3));
  627.  
  628.         *dmgptr += rnd(4);            /* 3d4 */
  629.  
  630.         if (otmp->spe > rn2(10))        /* probe */
  631.             attack_index = MB_INDEX_PROBE;
  632.         else {                    /* stun */
  633.             attack_index = MB_INDEX_STUN;
  634.             *dmgptr += rnd(4);        /* 4d4 */
  635.  
  636.             if (youdefend)
  637.                 make_stunned((HStun + 3), FALSE);
  638.             else
  639.                 mdef->mstun = 1;
  640.         }
  641.         if (dieroll <= scare_dieroll) {        /* scare */
  642.             attack_index = MB_INDEX_SCARE;
  643.             *dmgptr += rnd(4);        /* 5d4 */
  644.  
  645.             if (youdefend) {
  646.                 if (Antimagic)
  647.                     MB_RESIST_ATTACK;
  648.                 else {
  649.                     nomul(-3);
  650.                     nomovemsg = "";
  651. #ifdef POLYSELF
  652.                     if ((magr == u.ustuck)
  653.                         && sticks(uasmon)) {
  654.                         u.ustuck = (struct monst *)0;
  655.                         You("release %s!", mon_nam(magr));
  656.                     }
  657. #endif
  658.                 }
  659.             } else if (youattack) {
  660.                 if (rn2(2) && resist(mdef,SPBOOK_CLASS,0,0)) {
  661.                     MB_RESIST_ATTACK;
  662.                 } else {
  663.                     if (mdef == u.ustuck) {
  664.                     if (u.uswallow)
  665.                         expels(mdef,mdef->data,TRUE);
  666.                     else {
  667. #ifdef POLYSELF
  668.                         if (!sticks(uasmon))
  669. #endif
  670.                         {
  671.                         u.ustuck = (struct monst *)0;
  672.                         You("get released!");
  673.                         }
  674.                     }
  675.                     }
  676.                     mdef->mflee = 1;
  677.                     mdef->mfleetim += 3;
  678.                 }
  679.             }
  680.         }
  681.         if (dieroll <= (scare_dieroll / 2)) {    /* purge */
  682.             struct obj *ospell;
  683. #ifdef POLYSELF
  684.             struct permonst *old_uasmon = uasmon;
  685. #endif
  686.             attack_index = MB_INDEX_PURGE;
  687.             *dmgptr += rnd(4);        /* 6d4 */
  688.  
  689.             /* Create a fake spell object, ala spell.c */
  690.             ospell = mksobj(SPE_CANCELLATION, FALSE, FALSE);
  691.             ospell->blessed = ospell->cursed = 0;
  692.             ospell->quan = 20L;
  693.  
  694.             cancel_monst(mdef, ospell, youattack, FALSE, FALSE);
  695.  
  696. #ifdef POLYSELF
  697.             if (youdefend && (old_uasmon != uasmon))
  698.                 /* rehumanized, no more damage */
  699.                 *dmgptr = 0;
  700. #endif
  701.             if (youdefend) {
  702.                 if (Antimagic)
  703.                     MB_RESIST_ATTACK;
  704.             } else {
  705.                 if (!mdef->mcan)
  706.                     MB_RESIST_ATTACK;
  707.  
  708.                 /* cancelled clay golems will die ... */
  709.                 else if (mdef->data == &mons[PM_CLAY_GOLEM])
  710.                     mdef->mhp = 1;
  711.             }
  712.  
  713.             obfree(ospell, (struct obj *)0);
  714.         }
  715.  
  716.         if (youdefend || mdef->mhp > 0) {  /* ??? -dkh- */
  717.             static const char *mb_verb[4] =
  718.                 {"probe", "stun", "scare", "purge"};
  719.  
  720.             if (youattack || youdefend || vis) {
  721.                 pline("The magic-absorbing blade %ss %s!",
  722.                     mb_verb[attack_index], hittee);
  723.  
  724.                 if (MB_RESISTED_ATTACK) {
  725.                     pline("%s resist%s!",
  726.                     youdefend ? "You" : Monnam(mdef),
  727.                     youdefend ? "" : "s");
  728.  
  729.                     shieldeff(youdefend ? u.ux : mdef->mx,
  730.                         youdefend ? u.uy : mdef->my);
  731.                 }
  732.             }
  733.  
  734.             /* Much ado about nothing.  More magic fanfare! */
  735.             if (MB_UWEP_ATTACK) {
  736.                 if (attack_index == MB_INDEX_PURGE) {
  737.                     if (!MB_RESISTED_ATTACK &&
  738.                     attacktype(mdef->data, AT_MAGC)) {
  739.                     You("absorb magical energy!");
  740.                     u.uenmax++;
  741.                     u.uen++;
  742.                     flags.botl = 1;
  743.                     }
  744.                 } else if (attack_index == MB_INDEX_PROBE) {
  745.                     if (!rn2(4 * otmp->spe)) {
  746.                     pline("The probe is insightful!");
  747.                     /* pre-damage status */
  748.                     mstatusline(mdef);
  749.                     }
  750.                 }
  751.             } else if (youdefend && !MB_RESISTED_ATTACK
  752.                    && (attack_index == MB_INDEX_PURGE)) {
  753.                 You("lose magical energy!");
  754.                 if (u.uenmax > 0) u.uenmax--;
  755.                 if (u.uen > 0) u.uen--;
  756.                     flags.botl = 1;
  757.             }
  758.  
  759.             /* all this magic is confusing ... */
  760.             if (!rn2(12)) {
  761.                 if (youdefend)
  762.                 make_confused((HConfusion + 4), FALSE);
  763.                 else
  764.                 mdef->mconf = 1;
  765.  
  766.                 if (youattack || youdefend || vis)
  767.                 pline("%s %s confused.",
  768.                       youdefend ? "You" : Monnam(mdef),
  769.                       youdefend ? "are" : "is");
  770.             }
  771.         }
  772.         return TRUE;
  773.     }
  774.     /* end of Magicbane code */
  775.  
  776.     /* We really want "on a natural 20" but Nethack does it in */
  777.     /* reverse from AD&D. */
  778.     if (spec_ability(otmp, SPFX_BEHEAD)) {
  779. #ifdef MULDGN
  780.         if (otmp->oartifact == ART_TSURUGI_OF_MURAMASA && dieroll == 1) {
  781.         /* not really beheading, but so close, why add another SPFX */
  782.         if (youattack && u.uswallow && mdef == u.ustuck) {
  783.             You("slice %s wide open!", mon_nam(mdef));
  784.             *dmgptr = mdef->mhp;
  785.             return TRUE;
  786.         }
  787.         if (!youdefend) {
  788.             /* allow normal cutworm() call to add extra damage */
  789.             if(notonhead)
  790.                 return FALSE;
  791.  
  792.             if (bigmonst(mdef->data)) {
  793.                 if (youattack)
  794.                     You("slice deeply into %s!",
  795.                         mon_nam(mdef));
  796.                 else if (vis)
  797.                     pline("%s cuts deeply into %s!",
  798.                           Monnam(magr), mon_nam(mdef));
  799.                 *dmgptr *= 2;
  800.                 return TRUE;
  801.             }
  802.             *dmgptr = mdef->mhp;
  803.             pline("The razor-sharp blade cuts %s in half!",
  804.                   mon_nam(mdef));
  805.             otmp->dknown = TRUE;
  806.             return TRUE;
  807.         } else {
  808. #ifdef POLYSELF
  809.             if (bigmonst(uasmon)) {
  810.                 pline("%s cuts deeply into you!",
  811.                     Monnam(magr));
  812.                 *dmgptr *= 2;
  813.                 return TRUE;
  814.             }
  815. #endif
  816.             /* Players with negative AC's take less damage instead
  817.              * of just not getting hit.  We must add a large enough
  818.              * value to the damage so that this reduction in
  819.              * damage does not prevent death.
  820.              */
  821.             *dmgptr = u.uhp + 1234;
  822.             pline("The razor-sharp blade cuts you in half!");
  823.             otmp->dknown = TRUE;
  824.             return TRUE;
  825.         }
  826.         } else
  827. #endif /* MULDGN */
  828.         if (otmp->oartifact == ART_VORPAL_BLADE &&
  829.             (dieroll == 1 || mdef->data == &mons[PM_JABBERWOCK])) {
  830.  
  831.         static const char *behead_msg[2] = {
  832.              "%s beheads %s!",
  833.              "%s decapitates %s!"
  834.         };
  835.  
  836.         if (youattack && u.uswallow && mdef == u.ustuck)
  837.             return FALSE;
  838.         if (!youdefend) {
  839.             if (!has_head(mdef->data) || notonhead) {
  840.                 if (youattack)
  841.                     pline("Somehow, you miss %s wildly.",
  842.                         mon_nam(mdef));
  843.                 else if (vis)
  844.                     pline("Somehow, %s misses wildly.",
  845.                         mon_nam(magr));
  846.                 *dmgptr = 0;
  847.                 return ((boolean)(youattack || vis));
  848.             }
  849.             if (noncorporeal(mdef->data) || amorphous(mdef->data)) {
  850.                 pline("%s slices through %s neck.",
  851.                       artilist[ART_VORPAL_BLADE].name,
  852.                       s_suffix(mon_nam(mdef)));
  853.                 return ((boolean)(youattack || vis));
  854.             }
  855.             *dmgptr = mdef->mhp;
  856.             pline(behead_msg[rn2(SIZE(behead_msg))],
  857.                   artilist[ART_VORPAL_BLADE].name,
  858.                   mon_nam(mdef));
  859.             otmp->dknown = TRUE;
  860.             return TRUE;
  861.         } else {
  862. #ifdef POLYSELF
  863.             if (!has_head(uasmon)) {
  864.                 pline("Somehow, %s misses you wildly.",
  865.                     mon_nam(magr));
  866.                 *dmgptr = 0;
  867.                 return TRUE;
  868.             }
  869.             if (noncorporeal(uasmon) || amorphous(uasmon)) {
  870.                 pline("%s slices through your neck.",
  871.                       artilist[ART_VORPAL_BLADE].name);
  872.                 return TRUE;
  873.             }
  874. #endif
  875.             *dmgptr = u.uhp + 1234;
  876.             pline(behead_msg[rn2(SIZE(behead_msg))],
  877.                   artilist[ART_VORPAL_BLADE].name, "you");
  878.             otmp->dknown = TRUE;
  879.             /* Should amulets fall off? */
  880.             return TRUE;
  881.         }
  882.         }
  883.     }
  884.     if (spec_ability(otmp, SPFX_DRLI)) {
  885.         if (!youdefend && !resists_drli(mdef->data)) {
  886.             if (vis) {
  887.                 if(otmp->oartifact == ART_STORMBRINGER)
  888.                 pline("The %s blade draws the life from %s!",
  889.                       Hallucination ? hcolor() : Black,
  890.                       mon_nam(mdef));
  891.                 else
  892.                 pline("%s draws the life from %s!",
  893.                       The(distant_name(otmp, xname)),
  894.                       mon_nam(mdef));
  895.             }
  896.             if (mdef->m_lev == 0) *dmgptr = mdef->mhp;
  897.             else {
  898.                 int drain = rnd(8);
  899.                 *dmgptr += drain;
  900.                 mdef->mhpmax -= drain;
  901.                 mdef->m_lev--;
  902.                 drain /= 2;
  903.                 if (drain) healup(drain, 0, FALSE, FALSE);
  904.             }
  905.             return vis;
  906.         } else if (youdefend
  907. #ifdef POLYSELF
  908.                     && !resists_drli(uasmon)
  909. #endif
  910.                     && !defends(AD_DRLI, uwep)) {
  911.             if (Blind)
  912.                 You("feel an %s drain your life!",
  913.                     otmp->oartifact == ART_STORMBRINGER ?
  914.                     "unholy blade" : "object");
  915.             else {
  916.                 if(otmp->oartifact == ART_STORMBRINGER)
  917.                 pline("The %s blade drains your life!",
  918.                     Hallucination ? hcolor() : Black);
  919.                 else
  920.                 pline("%s drains your life!",
  921.                       The(distant_name(otmp, xname)));
  922.             }
  923.             losexp();
  924.             if (magr->mhp < magr->mhpmax) {
  925.                 magr->mhp += rnd(4);
  926.                 /* TODO: Should be related to # of HP you lost. */
  927.                 if (magr->mhp > magr->mhpmax) magr->mhp = magr->mhpmax;
  928.             }
  929.             return TRUE;
  930.         }
  931.     }
  932.     return FALSE;
  933. }
  934.  
  935. static const char recharge_type[] = { ALLOW_COUNT, ALL_CLASSES, 0 };
  936. static NEARDATA const char invoke_types[] =
  937.     { ALL_CLASSES, WEAPON_CLASS, ARMOR_CLASS, RING_CLASS, AMULET_CLASS,
  938.           TOOL_CLASS, 0 };
  939.  
  940. int
  941. doinvoke()
  942. {
  943.     register struct obj *obj;
  944.  
  945.     obj = getobj(invoke_types, "invoke");
  946.     if(!obj) return 0;
  947.     return arti_invoke(obj);
  948. }
  949.  
  950. STATIC_OVL int
  951. arti_invoke(obj)
  952.     register struct obj *obj;
  953. {
  954.     register const struct artifact *oart = get_artifact(obj);
  955.  
  956.     if(!oart || !oart->inv_prop) {
  957.     if(obj->otyp == CRYSTAL_BALL)
  958.         use_crystal_ball(obj);
  959.     else
  960.         pline("Nothing happens.");
  961.     return 1;
  962.     }
  963.  
  964.     if(oart->inv_prop > LAST_PROP) {
  965.     /* It's a special power, not "just" a property */
  966.     if(obj->age > monstermoves) {
  967.         /* the artifact is tired :-) */
  968.         You("feel that %s is ignoring you.", the(xname(obj)));
  969.         return 1;
  970.     }
  971.     obj->age = monstermoves + rnz(100);
  972.  
  973.     switch(oart->inv_prop) {
  974.     case TAMING: {
  975.         struct obj *pseudo = mksobj(SPE_CHARM_MONSTER, FALSE, FALSE);
  976.         pseudo->blessed = pseudo->cursed = 0;
  977.         pseudo->quan = 20L;            /* do not let useup get it */
  978.         (void) seffects(pseudo);
  979.         obfree(pseudo, (struct obj *)0);    /* now, get rid of it */
  980.         break;
  981.       }
  982.     case HEALING: {
  983.         int healamt = (u.uhpmax + 1 - u.uhp) / 2;
  984.         if(healamt || Sick || (Blinded > 1))
  985.         You("feel better.");
  986.         else
  987.         goto nothing_special;
  988.         if(healamt) u.uhp += healamt;
  989.         if(Sick) make_sick(0L,FALSE);
  990.         if(Blinded > 1) make_blinded(0L,FALSE);
  991.         flags.botl = 1;
  992.         break;
  993.       }
  994.     case ENERGY_BOOST: {
  995.         int epboost = (u.uenmax + 1 - u.uen) / 2;
  996.         if(epboost) {
  997.         You("feel re-energized.");
  998.         u.uen += epboost;
  999.         } else
  1000.         goto nothing_special;
  1001.         break;
  1002.       }
  1003.     case UNTRAP: {
  1004.         if(!untrap(TRUE)) {
  1005.         obj->age = 0; /* don't charge for changing their mind */
  1006.         return 0;
  1007.         }
  1008.         break;
  1009.       }
  1010.     case CHARGE_OBJ: {
  1011.         struct obj *otmp = getobj(recharge_type, "charge");
  1012.         if (!otmp) {
  1013.         obj->age = 0;
  1014.         return 0;
  1015.         }
  1016.         recharge(otmp, obj->blessed ? 1 : obj->cursed ? -1 : 0);
  1017.         break;
  1018.       }
  1019.     case LEV_TELE:
  1020.         level_tele();
  1021.         break;
  1022.     case CREATE_PORTAL: {
  1023.         register int i;
  1024.         d_level newlev;
  1025.         char buf[BUFSIZ];
  1026.         extern int n_dgns; /* from dungeon.c */
  1027.         winid tmpwin = create_nhwindow(NHW_MENU);
  1028.         char hc;
  1029.  
  1030.         start_menu(tmpwin);
  1031.         add_menu(tmpwin, 0, 0, "Dungeons:");
  1032.         add_menu(tmpwin, 0, 0, "");
  1033.         for (i = 0, hc = 'a'; i < n_dgns; i++) {
  1034.         if (!dungeons[i].dunlev_ureached) continue;
  1035.         Sprintf(buf, "%c - %s", hc, dungeons[i].dname);
  1036.         add_menu(tmpwin, hc, 0, buf);
  1037.         hc++;
  1038.         }
  1039.         add_menu(tmpwin, 0, 0, "");
  1040.         end_menu(tmpwin, '\033', "\033","Open a portal to which dungeon?");
  1041.         if (hc > 'b') {
  1042.         /* more than one entry; display menu for choices */
  1043.         hc = select_menu(tmpwin);
  1044.         } else
  1045.         hc = 'a';
  1046.         destroy_nhwindow(tmpwin);
  1047.  
  1048.         /* assume there won't be more than 26 dungeons */
  1049.         if (hc < 'a' || hc > 'z')
  1050.         goto nothing_special;
  1051.  
  1052.         /* run thru dungeon array to find the one they selected */
  1053.         for (i = 0; hc >= 'a'; i++)
  1054.         if (dungeons[i].dunlev_ureached) hc--;
  1055.         i--; /* we added one extra */
  1056.  
  1057.         /*
  1058.          * i is now index into dungeon structure for the new dungeon.
  1059.          * Find the closest level in the given dungeon, open
  1060.          * a use-once portal to that dungeon and go there.
  1061.          * The closest level is either the entry or dunlev_ureached.
  1062.          */
  1063.         newlev.dnum = i;
  1064.         if(dungeons[i].depth_start >= depth(&u.uz))
  1065.         newlev.dlevel = dungeons[i].entry_lev;
  1066.         else
  1067.         newlev.dlevel = dungeons[i].dunlev_ureached;
  1068.         if(u.uhave.amulet || In_endgame(&u.uz) || In_endgame(&newlev) ||
  1069.            newlev.dnum == u.uz.dnum) {
  1070.         You("feel very disoriented for a moment.");
  1071.         } else {
  1072.         if(!Blind) You("are surrounded by a shimmering sphere!");
  1073.         else You("feel weightless for a moment.");
  1074.         goto_level(&newlev, FALSE, FALSE, FALSE);
  1075.         }
  1076.         break;
  1077.       }
  1078.     }
  1079.     } else {
  1080.     long cprop = (u.uprops[oart->inv_prop].p_flgs ^= W_ARTI);
  1081.     boolean on = (cprop & W_ARTI) != 0; /* true if invoked prop just set */
  1082.  
  1083.     if(on && obj->age > monstermoves) {
  1084.         /* the artifact is tired :-) */
  1085.         u.uprops[oart->inv_prop].p_flgs ^= W_ARTI;
  1086.         You("feel that %s is ignoring you.", the(xname(obj)));
  1087.         return 1;
  1088.     } else if(!on) {
  1089.         /* when turning off property, determine downtime */
  1090.         /* arbitrary for now until we can tune this -dlc */
  1091.         obj->age = monstermoves + rnz(100);
  1092.     }
  1093.  
  1094.     if(cprop & ~W_ARTI) {
  1095.     nothing_special:
  1096.         /* you had the property from some other source too */
  1097.         if (carried(obj))
  1098.         You("feel a surge of power, but nothing seems to happen.");
  1099.         return 1;
  1100.     }
  1101.     switch(oart->inv_prop) {
  1102.     case CONFLICT:
  1103.         if(on) You("feel like a rabble-rouser.");
  1104.         else You("feel the tension decrease around you.");
  1105.         break;
  1106.     case LEVITATION:
  1107.         if(on) float_up();
  1108.         else (void) float_down();
  1109.         break;
  1110.     case INVIS:
  1111.         if (!See_invisible && !Blind) {
  1112.         newsym(u.ux,u.uy);
  1113.         if (on) {
  1114.             Your("body takes on a %s transparency...",
  1115.              Hallucination ? "normal" : "strange");
  1116.         } else {
  1117.             Your("body seems to unfade...");
  1118.         }
  1119.         } else goto nothing_special;
  1120.         break;
  1121.     }
  1122.     }
  1123.  
  1124.     return 1;
  1125. }
  1126.  
  1127. #endif /* OVLB */
  1128.  
  1129. /*artifact.c*/
  1130.